/**
 * @program: LeetCode
 * @description: LeetCode : 188. 买卖股票的最佳时机 IV
 * @author: WXY
 * @create: 2023-01-28 15:54
 * @Version 1.0
 **/
public class Num188_maxProfit {
    public static int maxProfit(int k, int[] prices) {
        int len = prices.length;
        int[][] dp = new int[len][2 * k + 1];
        for (int i = 1; i < 2 * k ; i += 2) {
            dp[0][i] = -prices[0];
        }
        for (int i = 1; i < len; i++) {
            for (int j = 0; j < 2 * k - 1; j += 2) {
                dp[i][j + 1] = Math.max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
                dp[i][j + 2] = Math.max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
            }
        }
        return dp[len - 1][2 * k];
    }

    public static void main(String[] args) {
        int[] arr = {3, 2, 6, 5, 0, 3};
        int k = 2;
        int i = maxProfit(k, arr);
        System.out.println(i);
    }
}
